local addon = LibStub("AceAddon-3.0"):GetAddon("Poppins")
local module = addon:NewModule("Options")

local L = LibStub("AceLocale-3.0"):GetLocale("Poppins")

local AceConfigDialog = LibStub("AceConfigDialog-3.0")
local AceConfigRegistry = LibStub("AceConfigRegistry-3.0")
local LibStickyFrames = LibStub("LibStickyFrames-2.0")
local LibKeyBound = LibStub("LibKeyBound-1.0")

local function open()
	AceConfigDialog:Open(addon.name)
end

function module:OnInitialize()
	AceConfigRegistry:RegisterOptionsTable(addon.name, addon.options)
	addon.db.RegisterCallback(self, "OnProfileChanged")
	addon.db.RegisterCallback(self, "OnProfileReset", "OnProfileChanged")
	addon.options.args.profile = LibStub("AceDBOptions-3.0"):GetOptionsTable(addon.db)
	addon:UnregisterChatCommand(addon.name)
	addon:RegisterChatCommand(addon.name, open)
end

function module:OnEnable()
	addon.Group.RegisterCallback(self, "OnCreate", "AddGroupOptions")
	addon.Frame.RegisterCallback(self, "OnSetup", "AddButtonOptions")
	self:OnProfileChanged()
	for name, modudle in addon:IterateModules() do
		if module ~= self and type(module.AddOptions) == "function" then
			module:AddOptions()
		end
	end
	open()
end

function module:OnProfileChanged()
	for name in pairs(addon.groups) do
		self:AddGroupOptions(nil, nil, name)
	end
	for i, button in ipairs(addon.buttons) do
		self:AddButtonOptions(nil, nil, i)
	end
end

do
	local function hidden(info) return not addon:GetGroup(info[#info]) end
	function module:AddGroupOptions(event, group, name)
		local options = addon.options.args.groups.args
		options[name] = options[name] or {
			name = format(L["Group: %s"], name),
			desc = format(L["Options for group: %s"], name),
			type = "group",
			childGroups = "tab",
			args = addon.Group.options,
			hidden = hidden,
		}
		AceConfigRegistry:NotifyChange(addon.name)
	end
end
do
	local function hidden(info) return not addon:GetButton(tonumber(info[#info])) end
	function module:AddButtonOptions(event, frame, id, db)
		local name = tostring(id)
		local options = addon.options.args.buttons.args
		options[name] = options[name] or {
			name = format(L["Button: %d"], id),
			desc = format(L["Options for button %d"], id),
			type = "group",
			childGroups = "tab",
			args = addon.Frame.options,
			hidden = hidden,
		}
		AceConfigRegistry:NotifyChange(addon.name)
	end
end

local function get(info)
	return addon.db.profile[info[#info]]
end
local function set(info, value)
	addon.db.profile[info[#info]] = value
	addon:UpdateButtons()
end

addon.options = {
	type = "group",
	handler = addon,
	plugins = {},
	disabled = InCombatLockdown,
	args = {
		enable = {
			name = L["Enable"],
			desc = L["Enables or disables Poppins"],
			type = "toggle",
			order = 1,
			set = function(info, value)
				if value then
					addon:Enable()
				else
					addon:Disable()
				end
			end,
			get = "IsEnabled",
		},
		lock = {
			name = L["Lock"],
			desc = L["Locks or unlocks all the buttons"],
			type = "toggle",
			order = 2,
			set = function(info, value)
				addon:SetLock(value)
			end,
			get = "IsLocked",
			disabled = InCombatLockdown,
		},
		buttonLock = {
			name = L["Button Lock"],
			desc = L["Locks or unlocks the buttons to allow or prevent spells/items/macros dragging"],
			type = "toggle",
			order = 3,
			set = function(info, value)
				addon.db.profile.lock = value
				for frame in addon.Frame:Iterate() do
					frame:UpdateHover()
				end
			end,
			get = function()
				return addon.db.profile.lock
			end,
		},
		assignBindings = {
			name = L["Key Bindings"],
			desc = L["Allows you to assign key bindings for the buttons"],
			type = "toggle",
			order = 4,
			get = function()
				return LibKeyBound:IsShown()
			end,
			set = function()
				return LibKeyBound:Toggle()
			end,
			disabled = InCombatLockdown,
		},
		global ={
			name = L["Global"],
			desc = L["Global options"],
			type = "group",
			order = 1,
			childGroups = "tab",
			plugins = {},
			args = {
				tooltips = {
					name = L["Tooltips"],
					desc = L["Tooltip options"],
					type = "select",
					get = get,
					set = set,
					values = { L["Disabled"], L["Enabled"], L["Disabled in Combat"], },
				},
			},
		},
		buttons = {
			name = L["Buttons"],
			desc = L["Buttons options"],
			type = "group",
			order = 6,
			args = {
				new = {
					name = L["New Button"],
					desc = L["Creates a new button"],
					type = "execute",
					func = "NewButton",
					order = 1,
				},
				delete = {
					name = L["Delete Button"],
					desc = L["Deletes a button"],
					type = "select",
					set = function(info, value)
						addon:DeleteButton(value)
					end,
					values = function(info)
						local t = {}
						for i in ipairs(addon.buttons) do
							t[i] = format(L["Button: %d"], i)
						end
						return t
					end,
					disabled = function()
						return addon.db.profile.count == 0
					end,
					order = 2,
				},
			},
		},
		groups ={
			name = L["Groups"],
			desc = L["Groups options"],
			type = "group",
			order = 7,
			args = {
				new = {
					name = L["New Group"],
					desc = L["Creates a new group"],
					type = "input",
					set = function(info, value)
						addon:NewGroup(value)
					end,
					order = 1,
				},
				delete = {
					name = L["Delete Group"],
					desc = L["Deletes a group"],
					type = "select",
					set = function(info, value)
						addon:DeleteGroup(value)
					end,
					values = function(info)
						local t = {}
						for group in pairs(addon.groups) do
							t[group] = format(L["Group: %s"], group)
						end
						return t
					end,
					disabled = function()
						return not next(addon.groups)
					end,
					order = 2,
				},
			},
		},
		modules = {
			name = L["Modules"],
			desc = L["Modules options"],
			type = "group",
			order = 8,
			args = {},
			plugins = {},
		},
	},
}